package cn.hznc.controller.service.process;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.EmployeeEntity;
import cn.hznc.domain.request.process.AddOrEditEmployeeReq;
import cn.hznc.exception.ToolManageEx;
import cn.hznc.exception.ToolManageException;
import cn.hznc.mapper.EmployeeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author：Administrator
 * @Date：2023/6/7 23:34
 */
@Service
public class EmployeeService{

    @Resource
    private EmployeeMapper employeeMapper;

    public Object addOrEditEmployee(AddOrEditEmployeeReq addOrEditEmployeeReq) throws ToolManageException {
        EmployeeEntity employeeEntity = new EmployeeEntity();
        BeanUtil.copyProperties(addOrEditEmployeeReq , employeeEntity);
        if(ObjectUtil.isNull(addOrEditEmployeeReq)){
            //新增
            employeeMapper.insert(employeeEntity);
            return Boolean.TRUE;
        }
        //查重员工编号
        LambdaQueryWrapper<EmployeeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmployeeEntity::getEmployeeNo,addOrEditEmployeeReq.getEmployeeNo());
        EmployeeEntity existEmployeeEntity = employeeMapper.selectOne(wrapper);
        if(ObjectUtil.isNull(existEmployeeEntity)){
            throw new ToolManageException(ToolManageEx.EMPLOYEE_NO_REPEAT.getCode(),ToolManageEx.EMPLOYEE_NO_REPEAT.getMsg());
        }
        employeeMapper.updateById(employeeEntity);
        return Boolean.TRUE;
    }

    public Object deleteEmployee(Integer id) {
        QueryWrapper<EmployeeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        employeeMapper.delete(wrapper);
        return Boolean.TRUE;
    }

    public Object selectEmployees() {
        return employeeMapper.selectList(new QueryWrapper<>());
    }
}
